package com.zhang.test2;


import org.apache.log4j.*;
import org.apache.log4j.helpers.LogLog;
import org.junit.Test;

public class Hardcode {

    //创建日志对象3种方式
    @Test
    public void test2() {
        Logger logger1 = Logger.getLogger("com.zhang.test2.Hardcode");
        Logger logger2 = Logger.getLogger(Hardcode.class.getName());
        Logger logger3 = Logger.getLogger(Hardcode.class);

        System.out.println(Hardcode.class);//class com.zhang.test2.Hardcode
        System.out.println(Hardcode.class.getName());//com.zhang.test2.Hardcode

    }



    //但日志对象-硬编码
    @Test
    public void test4() {

        //1.configue()方法创建了rootLogger根节点日志对象,并自动配置了ConsoleAppender+布局
        BasicConfigurator.configure();




        //1.创建日志对象
        Logger logger1 = Logger.getLogger("com.zhang.test2.Hardcode");
        //2.配置logger1日志对象的输出级别
        logger1.setLevel(Level.WARN);

        logger1.setAdditivity(false);



        //3.创建appender
        ConsoleAppender consoleAppender = new ConsoleAppender();
        consoleAppender.setThreshold(Level.WARN);//设置consoleAppender的打印级别
        PatternLayout patternLayout = new PatternLayout("+ %m%n");//创建布局对象
        consoleAppender.setLayout(patternLayout);//appender设置日志打印的格式

        //4.logger1添加要使用的appender(日志处理器对象)
        logger1.addAppender(consoleAppender);


        //测试
        logger1.trace("trace");
        logger1.debug("debug");
        logger1.info("info");
        logger1.warn("warn");
        logger1.error("error");
        logger1.fatal("fatal");

    }


    //硬编码-三层继承
    @Test
    public void test1() {
        //1.配置RootLogger
        Logger root = Logger.getRootLogger();
        root.addAppender(new ConsoleAppender(new PatternLayout("- %m%n")));
        root.setLevel(Level.FATAL);



        //2.配置自定义Logger1
        Logger logger1 = Logger.getLogger("com.zhang");
        logger1.setLevel(Level.INFO);





        //3.配置自定义Logger2
        Logger logger2 = Logger.getLogger("com.zhang.test2.Hardcode");




        logger2.trace("trace");
        logger2.debug("debug");
        logger2.info("info");
        logger2.warn("warn");
        logger2.error("error");
        logger2.fatal("fatal");

    }

    //打印日志的详细信息
    @Test
    public void test3() {

        LogLog.setInternalDebugging(true);

        //1.配置RootLogger
        Logger root = Logger.getRootLogger();
        root.addAppender(new ConsoleAppender(new PatternLayout("- %m%n")));
        root.setLevel(Level.FATAL);

        //2.配置自定义Logger1
        Logger logger1 = Logger.getLogger("com.zhang");
        logger1.setLevel(Level.INFO);


        //3.配置自定义Logger2
        Logger logger2 = Logger.getLogger("com.zhang.test2.Hardcode");

        logger2.trace("trace");
        logger2.debug("debug");
        logger2.info("info");
        logger2.warn("warn");
        logger2.error("error");
        logger2.fatal("fatal");

    }





}

